package com.gun3y.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

import com.gun3y.entity.media.PropertyType;
import com.gun3y.entity.result.ResultField;
import com.gun3y.exception.ExceptionCode;
import com.gun3y.exception.SWException;

public class Commons {

    public static final String WRAPPER = "http://www.gun3y.com/ontology/wrapper#";
    public static final String MOVIE = "http://www.gun3y.com/ontology/movie#";
    public static final String RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
    public static final String RDFS = "http://www.w3.org/2000/01/rdf-schema#";
    public static final String OWL = "http://www.w3.org/2002/07/owl#";
    public static final String XSD = "http://www.w3.org/2001/XMLSchema#";

    public static final String CONS_WRAPPER = "wrapper";

    public static final String DEFAULT_ENCODING = "UTF-8";

    private static final String DATE_PATTERN = "";

    private static final String NUMBER_PATTERN = "[0-9,\\.\\+-]+";

    public static String cleanText(String str) {
	if (StringUtils.isBlank(str)) {
	    return StringUtils.EMPTY;
	}

	return str.trim().replaceAll(" ", "~").replaceAll("\\s+", "").replaceAll("~", " ").replaceAll("\"", "");
    }

    public static List<ResultField> sublist(List<ResultField> resultList, PropertyType propertyType) {
	List<ResultField> retList = new ArrayList<ResultField>();

	if (resultList == null || propertyType == null) {
	    return retList;
	}
	for (ResultField resultField : resultList) {
	    if (resultField.getName() == propertyType) {
		retList.add(resultField);
	    }
	}
	return retList;
    }

    public static String extractDate(String dateVal, String dateFormat) {

	String retVal = "";
	String regexPattern = dateFormat.replaceAll("[mHsSdywWkK]", "\\\\d");

	if (regexPattern.contains("M")) {
	    int sIdx = regexPattern.indexOf('M');
	    int pivotIdx = sIdx;

	    while (regexPattern.length() > (pivotIdx + 1)) {
		char letter = regexPattern.charAt(++pivotIdx);
		if (letter != 'M') {
		    break;
		}
	    }
	    int size = pivotIdx - sIdx;

	    if (size <= 2) {
		regexPattern = regexPattern.replaceAll("[M]", "\\\\d");
	    }
	    else {
		regexPattern = regexPattern.replaceAll("[M]", "\\\\D");
	    }

	}
	Pattern pattern = Pattern.compile(regexPattern);
	Matcher matcher = pattern.matcher(dateVal);

	if (matcher.find()) {
	    retVal = transformDate(matcher.group(), dateFormat);
	}

	return retVal;
    }

    public static String transformDate(String dateVal, String pattern) {
	String retVal = StringUtils.EMPTY;
	DateFormat df = new SimpleDateFormat(pattern);
	Date date;
	try {
	    date = df.parse(dateVal);
	    SimpleDateFormat fmt = new SimpleDateFormat(DATE_PATTERN);
	    retVal = fmt.format(date);
	}
	catch (ParseException e) {
	    e.printStackTrace();
	}

	return retVal;
    }

    public static String extractNumber(String val) {
	String retVal = "";
	Pattern p = Pattern.compile(NUMBER_PATTERN);
	Matcher m = p.matcher(val);

	if (m.find()) {
	    retVal = m.group();
	}
	return retVal;
    }

    public static String encode(String str) {

	if (str == null) {
	    return StringUtils.EMPTY;
	}

	try {
	    return URLEncoder.encode(str, DEFAULT_ENCODING);
	}
	catch (UnsupportedEncodingException e) {
	    e.printStackTrace();
	    throw new SWException(ExceptionCode.UnknownError);
	}
    }

    public static String decode(String str) {

	if (str == null) {
	    return StringUtils.EMPTY;
	}

	try {
	    return URLDecoder.decode(str, DEFAULT_ENCODING);
	}
	catch (UnsupportedEncodingException e) {
	    e.printStackTrace();
	    throw new SWException(ExceptionCode.UnknownError);
	}
    }

}
